var FileMsgSender = (function(){    
    var _FileMsgSender = (function(){}).extend(MsgSender);
    
    _FileMsgSender.prototype.send = function(cb){
        var _this = this,
            fileMeta = this.content;
        
        var chatGroupEntity = page.chatGroupStore.getItem(_this.chatGroupID);
        var fileData = {
            contentType : MsgType.FILE,                            
            teid : _this.chatGroupID,                        
            content : {
                name : fileMeta.file.name,
                length : fileMeta.file.size,    
            },                                                
            fileUrl : URL.createObjectURL(fileMeta.file)
        };
        
        if(chatGroupEntity.perm.rloc && page.position){
            fileData['la'] = page.position.coords.latitude;
            fileData['lo'] = page.position.coords.longitude;
        }
                    
        //呈现FileMsg.
        var fileMsg = (new ChatFileMsg(fileData, 'right')).render(_this.chatPanel.chatView).showProgressBar();
        fileMsg.markSending();
        
        var flag = false;
        if(fileMsg.canShowReaders()){
            flag = true;
            fileMsg.disableShowReaders();
        }
                
        var api = '/rest/apis/chat/{0}/rupload?type={1}'.format(_this.chatGroupID, MsgType.FILE);
        var r = new ujs.subjects.upload.Resumable({
            target : api,            
            method : "octet"
        });
        
        fileMsg.progressBar.on('cancel', function(rate){
            r.cancel();
            fileMsg.$wrapper.remove();
        });
        
        r.api = api;               
        r.on('fileAdded', function(file) {                
            r.isComplete = false;                
            r.upload();
        });
        // r.on('cancel', function() {
            // alert('canceled');
        // });
        r.on('pause', function() {});
        r.on('complete', function() {  
            fileMsg.progressBar.setValue(100);
            fileMsg.hideProgressBar();     
            fileMsg.clearSending();         
            r.isComplete = true;
        });
        r.on('fileSuccess', function(file, msg) {
            if(_this.chatPanel.chatGroupEntity.type == 'singlechat'){
                fileMsg.setUnread();
            }
            msg = msg.toJSON();
            page.chatMsgStore.addItem({
                local : true,
                teid : _this.chatGroupID,
                seq : msg['seq'],
                createTime : msg['time'],
                contentType : MsgType.FILE,
                from : 'web',
                id : msg['id'],
                content : {
                    name : fileMeta.file.name,
                    length : fileMeta.file.size,
                    rid : msg['rid'] 
                },
                user : _this.user
            });
            fileMsg.updateSeq(msg['seq']).updateMsgTime(msg['time']);  
            fileMsg.adjustPos(msg['seq']).chatView.refreshReadCount();    
            fileMsg.$wrapper.find('.fileMsgWrapper').attr(
                'fileUrl', 
                '/rest/apis/chat/{topicEntityId}/topic/{topicId}/upload/{resourceId}/{fileName}'.format({
                    topicEntityId : _this.chatGroupID,
                    topicId : msg['id'],
                    resourceId : msg['rid'],
                    fileName : fileMeta.file.name
                })
            );      
            if(flag){
                fileMsg.enableShowReaders();
            }    
            cb && cb(msg);
        });
        r.on('fileError', function(file) {
            fileMsg.markFailed();
            fileMsg.$retryBtn.click(function(){
                fileMsg.hide();
                _this.send(function(){
                    _this.chatPanel.chatView.$wrapper.scrollToEnd();
                });
                return false;
            });
        });
        r.on('fileProgress', function(file) {  
            var percentComplete = r.progress();            
            fileMsg.progressBar.setValue(percentComplete.toFixed(0) * 100);
        });
        
        r.addFile(fileMeta.file);
    };
    
    return _FileMsgSender;
})();
